//
//  YSPicture.swift
//  新浪浪
//
//  Created by mac on 16/3/3.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SDWebImage



let ItemMargin:CGFloat = 10

let ScreenWH : CGFloat = UIScreen .mainScreen().bounds.width

let ItemWH = (ScreenWH - 4 * ItemMargin)/3

let YSPictureViewCollectionViewReuserIdentifer = "SYPictureViewCollectionViewReuserIdentifer"

class YSPicture: UICollectionView,UICollectionViewDataSource {
//    
//    var pictureViewModel: YSStatusViewModel? {
//       
//        didSet{
//            //更新配图约束
//            self.snp_makeConstraints { (make) -> Void in
//                let size = self.calPictureSize()
////                make.size.equalTo(self.calPictureSize())
//                make.width.equalTo(size.width)
//                make.height.equalTo(size.height)
//                print(size)
//            }
//            reloadData()
//        }
//    }
//    
    
    var pictureList: [YSStatusViewModel]?{
        didSet{
            self.snp_updateConstraints { (make) -> Void in
                let size = self.calPictureSize()
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            addSubview(label)
            label.snp_makeConstraints { (make) -> Void in
                make.center.equalTo(self.snp_center)
            }
            label.text = "\(pictureList?.count ?? 0)"
            reloadData()
        }
    }
    
    

    var flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    private func setupUI(){
    
        backgroundColor = UIColor .whiteColor()
        self.dataSource = self
        //注册cell
        self.registerClass(YSPictureCollectionViewCell.self, forCellWithReuseIdentifier: YSPictureViewCollectionViewReuserIdentifer)
        //设置水平
//        flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
        flowLayout.itemSize = CGSizeMake(ItemWH-1, ItemWH-1)
        
        flowLayout.minimumLineSpacing = ItemMargin
        
        flowLayout.minimumInteritemSpacing = ItemMargin
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }

    private func calPictureSize()->CGSize{
        let count = pictureList?.count
        if count <= 3 {
            return CGSizeMake(ItemWH * 3 + 2 * ItemMargin, ItemWH)
        }else if count == 4 {
            return CGSizeMake(ItemWH * 2 + ItemMargin, ItemWH * 2 + ItemMargin)
        }else {
            let row = CGFloat((count! - 1)/3 + 1)
            
            return CGSizeMake(3 * ItemWH + 2 * ItemMargin, ItemWH * row + ItemMargin * row )
        }
    }
    //添加一个测试lab
    private lazy var label:UILabel = {
        let lab = UILabel()
        return lab
        }()
    
}

//计算配图视图的宽高




extension YSPicture{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureList?.count ?? 0
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YSPictureViewCollectionViewReuserIdentifer, forIndexPath: indexPath) as! YSPictureCollectionViewCell
        
        cell.backgroundColor = UIColor ( red: CGFloat(random()%255)/255.0,green:CGFloat(random()%255)/255.0,blue:CGFloat(random()%255)/255.0,alpha :1)
        
        
//        let viewModel = YSPictureViewModel()
//        viewModel.model = pictureViewModel?.statusModel?.pic_urls![indexPath.item]
        
        cell.pictureViewModel = pictureList![indexPath.item].picture
        
        return cell
    }
    
    
}

class YSPictureCollectionViewCell:UICollectionViewCell{
    
    var pictureViewModel:YSPictureModel?{
        didSet{
            let url = NSURL (string: pictureViewModel?.thumbnail_pic ?? "")
            if let Ourl = url {
                iconImageView.sd_setImageWithURL(Ourl)
            }
        }
    }
    
    
    //三部曲
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        addSubview(iconImageView)
        iconImageView.frame = self.bounds
    }
    
    
    private lazy var iconImageView: UIImageView = {
       let ima = UIImageView()
        return ima
    }()
    
    
}









